package com.lqwu.mallchatserver.user.controller;

import com.lqwu.mallchatserver.common.domain.vo.response.ApiResult;
import com.lqwu.mallchatserver.common.utils.AssertUtil;
import com.lqwu.mallchatserver.common.utils.RequestHolder;
import com.lqwu.mallchatserver.user.model.dto.*;
import com.lqwu.mallchatserver.user.model.enums.RoleEnum;
import com.lqwu.mallchatserver.user.model.vo.BadgeResp;
import com.lqwu.mallchatserver.user.model.vo.UserInfoResp;
import com.lqwu.mallchatserver.user.service.RoleService;
import com.lqwu.mallchatserver.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/capi/user")
@Slf4j
@Api(tags = "用户相关接口")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;

    @GetMapping("/userInfo")
    @ApiOperation("获取用户个人信息")
    public ApiResult<UserInfoResp> getUserInfo(){
        Long  uid = RequestHolder.get().getUid();
        return ApiResult.success(userService.getUserInfo(uid));
    }

    @PostMapping("/public/summary/userInfo/batch")
    @ApiOperation("用户聚合信息-返回的代表需要刷新的")
    public ApiResult<List<SummeryInfoDTO>> getSummeryUserInfo(@Valid @RequestBody SummeryInfoReq req) {
        return ApiResult.success(userService.getSummeryUserInfo(req));
    }

    @PostMapping("/public/badges/batch")
    @ApiOperation("徽章聚合信息-返回的代表需要刷新的")
    public ApiResult<List<ItemInfoDTO>> getItemInfo(@Valid @RequestBody ItemInfoReq req) {
        return ApiResult.success(userService.getItemInfo(req));
    }

    @PutMapping("/name")
    @ApiOperation("修改用户昵称")
    public ApiResult<Void> modifyName(@RequestBody ModifyNameReq req) throws InterruptedException {
        userService.modifyName(RequestHolder.get().getUid(),req.getName());
        return ApiResult.success();
    }

    @GetMapping("/badges")
    @ApiOperation("可选徽章列表")
    public ApiResult<List<BadgeResp>> badges(){
        List<BadgeResp> badges = userService.badges(RequestHolder.get().getUid());
        return ApiResult.success(badges);
    }

    @PutMapping("/badge")
    @ApiOperation("佩戴徽章")
    public ApiResult<Void> wearingBadge(@RequestBody WearingBadgeReq req)  {
        userService.wearingBadge(req.getItemId(),RequestHolder.get().getUid());
        return ApiResult.success();
    }

    @PutMapping("/black")
    @ApiOperation("用户拉黑")
    public ApiResult<Void> black(@RequestBody BlackReq req)  {
        Long uid = req.getUid();// 要拉黑的用户id
        Long currentUid = RequestHolder.get().getUid();// 当前用户id
        // 获取当前用户是否有权限
        Boolean hasPower = roleService.hasPower(currentUid, RoleEnum.ADMIN.getId());
        AssertUtil.isTrue(!hasPower, "非管理员，无权限 哦");
        userService.black(uid);
        return ApiResult.success();
    }
}
